package com.bdcom.autotest.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bdcom.autotest.MainFrame;
import com.bdcom.autotest.interaction.step.StepBean;
import com.bdcom.autotest.ui.step.StepItem;
import com.bdcom.autotest.util.Prop;
import com.bdcom.autotest.util.UIUtil;
import com.bdcom.autotest.util.VerticalFlowLayout;

public class CenterPanel extends JPanel{

	private static Logger log = LoggerFactory.getLogger(CenterPanel.class);
	
	private MainFrame mf;
	private JPanel panel ;
	private 	JScrollPane scroll;
	
	private List<StepBean> steps = new ArrayList<StepBean>();
	private List<StepBean> toRuns = new ArrayList<StepBean>();
	
	public CenterPanel( MainFrame mf ) {
		this.mf = mf;
		
		
		this.setPreferredSize(new Dimension( 540 , 0 ) );
		
		this.setLayout(new BorderLayout() );
		
		panel = new JPanel( );
		scroll = new JScrollPane(panel);
		scroll.setBorder( BorderFactory.createEmptyBorder() );
		this.add(scroll);
		initComponent();
		UIUtil.setCompBack(panel);
		UIUtil.setCompBack(this);
		
	}

	private void initComponent() {
		
		
		panel.setLayout( new VerticalFlowLayout() );
		
	}
	
	public List<StepBean> getSteps() {
		return steps;
	}

	public void setSteps( List<StepBean> steps ) {
		this.steps = steps;
		panel.removeAll();
		toRuns.clear();
		int counter = 0;
		int size = steps.size();
		for(  int i=0; i<size ; i++) {
			StepBean stepBean = steps.get(i);
			StepItem stepItem = new StepItem(stepBean , counter );
			if( StepBean.type_step.equals(stepBean.getType() )  ) {
				toRuns.add(stepBean);
				counter++;
			}
			panel.add( stepItem );
		}
		
		this.validate();
		this.revalidate();
		this.repaint();
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				JScrollBar verticalScrollBar = scroll.getVerticalScrollBar();
				int value = verticalScrollBar.getMaximum();
				verticalScrollBar.setValue( 0 );
			}
		});
		
	}

	public List<StepBean> getToRuns() {
		return toRuns;
	}

	public void setToRuns(List<StepBean> toRuns) {
		this.toRuns = toRuns;
	}

	public void scrollFlow( int value ) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				JScrollBar verticalScrollBar = scroll.getVerticalScrollBar();
				verticalScrollBar.setValue( value );
			}
		});
		
	}

	public void resetTimes() {
		for(   StepBean ss : steps ) {
			ss.setRunTimes(0);
		}
	}
	
	
	
	
}
